package blackitheima1.Binarysearch;
//二分法在从小到大排列数组中查找指定数字
public class MyBinarySearch {
    public static void main(String[] args) {
        //定义数组及要查找的数
        int[]arr={1,2,3,4,5,6,7,8,9,10};
        int number=9;
        //调用方法
       int index= getIndex(arr,number);
        System.out.println(index);

    }

    private static int getIndex(int[] arr, int number) {
        //定义最值索引
        int min=0;
        int max=arr.length-1;
        //条件循环查找
        while(min<=max){
            //中值索引mid
            int mid=(min+max)>>1;//(min+max)/2
            //arr[mid]>number  number 在左边
            if(arr[mid]>number){
                max=mid-1;
            }else if(arr[mid]<number){
                //arr[mid]<number number 在右边
                min=mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
